package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessTrainingVideo;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.toolkit.RegexpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.digitalchina.lxnsy.xml.ExpertInfoXML;
import com.digitalchina.lxnsy.xml.PersonalInfoXML;
import com.digitalchina.lxnsy.xml.UserXML;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/5/21 20:17
 * 修改备注：
 * ================================================
 */
public class TechnologyTrainingVideoSignUpActivity extends BaseActivity {
    @Bind(R.id.edit_address_et_name)
    EditTextWithClear editAddressEtName;
    @Bind(R.id.edit_address_et_phone)
    EditTextWithClear editAddressEtPhone;
    @Bind(R.id.edit_address_tv_area)
    TextView editAddressTvArea;
    @Bind(R.id.edit_address_tv_area_hint)
    TextView editAddressTvAreaHint;
    @Bind(R.id.edit_address_et_detail_address)
    EditTextWithClear editAddressEtDetailAddress;
    @Bind(R.id.edit_address_tv_name)
    TextView editAddressTvName;
    @Bind(R.id.edit_address_btn_save)
    Button editAddressBtnSave;

    private String agriClassesId = "";

    String provinceName;
    String cityName;
    String countyName;
    String detailsName;

    String provinceCode = "", cityCode = "", countyCode = "";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_edit_address);
        setTitle("直播报名");
        editAddressTvName.setText("姓        名：");
        editAddressBtnSave.setText("提交");
    }

    @Override
    public void initMember() {

        agriClassesId = (String) getIntent().getSerializableExtra("agriClassesId");

        if (UserXML.getUserType(this).equals(Constant.USER_TYPE_PERSON))
            editAddressEtName.setText(PersonalInfoXML.getTrueName(this));
        else
            editAddressEtName.setText(ExpertInfoXML.getExpertName(this));

        String s = editAddressEtName.getText().toString();
        if (!StringUtil.isStrEmpty(s))
            editAddressEtName.setSelection(s.length());
        editAddressEtPhone.setText(UserXML.getMobilePhone(this));

    }


    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_SAVE_SIGN_UP_INFO_SUCCESS:
                        new MaterialDialog.Builder(TechnologyTrainingVideoSignUpActivity.this)
                                .title("提示")
                                .content("报名成功！")
                                .positiveText("确定")
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        finish();
                                    }
                                })
                                .show();
                        break;
                    case MSG.MSG_SAVE_SIGN_UP_INFO_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick({R.id.edit_address_tv_area, R.id.edit_address_btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_address_tv_area:
                final BottomDialog bottomDialog = new BottomDialog(this);
                bottomDialog.setOnAddressSelectedListener(new OnAddressSelectedListener() {
                    @Override
                    public void onAddressSelected(Province province, City city, County county, Street street) {
                        provinceCode = province.province_code;
                        cityCode = city != null ? city.city_code : "";
                        countyCode = county != null ? county.county_code : "";
                        editAddressTvAreaHint.setVisibility(View.GONE);
                        if (provinceCode.equals("110000") || provinceCode.equals("120000") || provinceCode.equals("310000") || provinceCode.equals("500000") || (provinceCode.equals("460000") && cityCode.equals("469000")))
                            editAddressTvArea.setText(String.format("%s %s", province.province_name, county != null ? county.county_name : ""));
                        else
                            editAddressTvArea.setText(String.format("%s %s %s", province.province_name,
                                    city != null ? city.city_name : "", county != null ? county.county_name : ""));
                        bottomDialog.dismiss();
                    }
                });
                bottomDialog.show();
                break;
            case R.id.edit_address_btn_save:
                if (StringUtil.isEditEmpty(editAddressEtName)) {
                    showSnackbar("姓名不能为空");
                    return;
                }
                if (StringUtil.isEditEmpty(editAddressEtPhone)) {
                    showSnackbar("手机号码不能为空");
                    return;
                }

                if (!RegexpUtil.isRegexpValidate(editAddressEtPhone.getText().toString(), RegexpUtil.MOBILE_REGEXP)) {
                    showSnackbar("请输入合法手机号码");
                    return;
                }

                if (StringUtil.isStrEmpty(editAddressTvArea.getText().toString().trim())) {
                    showSnackbar("所在地区不能为空");
                    return;
                }
                if (StringUtil.isEditEmpty(editAddressEtDetailAddress)) {
                    showSnackbar("详细地址不能为空");
                    return;
                }

                BusinessTrainingVideo.saveSignUpInfo(TechnologyTrainingVideoSignUpActivity.this, agriClassesId, StringUtil.getEditText(editAddressEtName), StringUtil.getEditText(editAddressEtPhone), StringUtil.getEditText(editAddressEtDetailAddress), provinceCode, cityCode, countyCode, mHandler);
                break;
        }
    }
}
